package com.javalong.affairs.utils;

import java.io.File;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Environment;

public class StringUtils {
	public static final int SMSCODE_LENGTH = 6;

	public static final int PWD_MIN_LENGTH = 6;

	public static final int PWD_MAX_LENGTH = 12;

	public static final String FILE_URI_PREFX = "file://";

	private static final File FILE_PREFIX = Environment
			.getExternalStorageDirectory().getAbsoluteFile();
	private static final String APP_PREFIX = "affairs";

	/**
	 * @author ygh 2015/4/27
	 * 
	 * 
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^(1[3578])[0-9]{9}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	/**
	 * @author ygh 2015/4/28
	 * 
	 */
	public static boolean isSmsCode(String smsCode) {
		if (smsCode != null && smsCode.length() == SMSCODE_LENGTH) {
			return true;
		}
		return false;
	}

	/**
	 * @author ygh 2015/4/28
	 * 
	 */
	public static boolean verifyPwd(String pwd) {
		if (pwd != null && pwd.length() >= PWD_MIN_LENGTH) {
			return true;
		}
		return false;
	}

	/**
	 * @author ygh 2015/4/28
	 * 
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str)) {
			return true;
		}
		return false;
	}

	public static String getSplitString(String str, String split, int index) {
		String[] strArr = str.split(split);
		if (index < 0) {
			if (strArr.length + index < 0) {
				return "";
			} else {
				return strArr[strArr.length + index];
			}
		} else {
			return strArr[index];
		}
	}

	public static String removeFilePrefix(String path) {
		if (!isEmpty(path) && path.startsWith(FILE_URI_PREFX)) {
			return path.substring(FILE_URI_PREFX.length());
		}
		return null;
	}

	/**
	 */
	public static String changeImageParam(String url, String param) {
		if (url != null) {
			if (url.contains("?")) {
				StringBuilder sb = new StringBuilder(url.substring(0,
						url.indexOf("?")));
				return sb.append(param).toString();
			}
			return url + param;
		} else {
			return null;
		}
	}

	/**
	 * 
	 */
	public static String changeNum(String str, int flag) {
		String strs[] = str.split(" ");
		String text = Integer.parseInt(strs[1]) + (flag == 1 ? 1 : -1) + "";
		return strs[0] + " " + text;
	}

	/**
	 * 瀛楃涓� 鍘绘帀鏁板瓧 鍔犱笂鍙傛暟
	 * 
	 */
	public static String changeText(String str, String num) {
		String strs[] = str.split(" ");
		return strs[0] + " " + num;
	}

	/**
	 * 
	 */
	public static String changeText(String str, long num) {
		String strs[] = str.split(" ");
		return strs[0] + " " + num;
	}

	/**
	 */
	public static int getNum(String str) {
		String strs[] = str.split(" ");
		return Integer.parseInt(strs[1]);
	}

	public static String trim(String name) {
		return name.trim();
	}

	public static String getRandomFilePath() {
		String uid = UUID.randomUUID().toString();
		if (FILE_PREFIX.isDirectory()) {
			return FILE_PREFIX + "/" + APP_PREFIX + "/" + uid;
		} else {
			return "/" + APP_PREFIX + "/" + uid;
		}
	}
}
